package com.jing.weibo.controller.cw;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import weibo4j.model.WeiboException;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.WeiboCons;
import com.jing.system.utils.StringUtil;
import com.jing.weibo.pojo.user.UserCwMp;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.user.UserCwMpService;
import com.jing.weibo.ws.sina.FriendshipsService;

/**
 * 同城互粉的Controller
 * @author jing.yue
 * @version 2012/11/20 1.0.0
 */
@Controller
public class CwController extends BaseController {

	private static final Logger logger = Logger.getLogger(CwController.class);

	@Autowired
	private UserCwMpService userCwMpService;
	@Autowired
	private FriendshipsService friendshipsService;

	/**
	 * 分页获取登录用户同城的用户的列表
	 * @return
	 */
	@RequestMapping(value = "/cw/json/findByUserInfo")
	@ResponseBody
	public ModelMap findByUserInfo(HttpServletRequest request, UserCwMp userCwMp) {
		String result = null;
		Map<String, Object> map = null;
		UserInfo userInfo = getSessionUser(request);
		try {
			userCwMp.setUsername(userInfo.getUsername());
			userCwMp.setLocation(userInfo.getLocation());
			map = userCwMpService.findByUserInfo(userCwMp);
			result = SUCCESS;
		} catch (Exception e) {
			logger.error(userInfo.getNickname() + ": 分页获取登录用户同城的用户的列表异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", map);
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 关注用户
	 * @return
	 */
	@RequestMapping(value = "/cw/json/attentionUser")
	@ResponseBody
	public ModelMap attentionUser(HttpServletRequest request, String uids) {
		String result = null;
		Integer errorWeiboCode = null;
		StringBuffer errorWeiboMsg = new StringBuffer();
		UserInfo userInfo = getSessionUser(request);
		if(userInfo.getIsopencitywidemp().intValue() == 0) {
			result = ERROR_ISOPENCWMP;
		} else {
			int count = 0;
			int errorCount = 0;
			String[] uidArr = uids.split(",");
			for (String uid : uidArr) {
				if(StringUtil.isEmpty(uid)) {
					continue;
				}
				try {
					friendshipsService.createFriendshipsById(uid, userInfo.getToken());
				} catch (WeiboException e) {
					logger.error(WeiboCons.getErrorMsg(e));
					result = ERROR_WEIBO;
					errorWeiboMsg.append("调用微博接口关注[").append(uid).append("]用户出现异常[").append(WeiboCons.getErrorMsg(e)).append("]!");
					errorWeiboCode = e.getErrorCode();
					//用户不存在或token过期或access_token 无效
					if(StringUtil.isEqualArr(e.getErrorCode(), 20003, 21327, 21332)) {
						try {
							//根据用户名删除同城互粉信息
							userCwMpService.deleteByUsername(uid);
						} catch (Exception e1) {
							logger.error("处理用户不存在或token过期或access_token 无效的信息异常!", e);
						}
					}
					errorCount ++;
				} catch (Exception e) {
					logger.error("关注用户异常!", e);
					errorCount ++;
				}
				count ++;
			}
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("errorWeiboCode", errorWeiboCode);
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		return modelMap;
	}

}